﻿using System;
using System.Collections.Generic;
using System.Linq;
using Rodunu.Business.Interfaces;
using Rodunu.Business.Models.User;

namespace Rodunu.Business.Logic
{
    public class Login : ILogin 
    {
        private readonly IManageSettings _manageSettings;
        
        public Login(IManageSettings manageSettings)
        {
            _manageSettings = manageSettings;
        }

        #region ILogin Members

        public User LoginUserOn(string userName, string password, bool rememberUser, bool rememberPassword, bool autoLogIn)
        {
            User currentUser = new User();

            if (_manageSettings.CurrentSettings.Users != null)
                currentUser = (from user in _manageSettings.CurrentSettings.Users
                                 where user.UserName == userName && user.Password == password
                                 select user).SingleOrDefault();

            _manageSettings.CurrentSettings.AutoLogin = autoLogIn;
            _manageSettings.CurrentSettings.RememberMe = rememberUser;
            _manageSettings.CurrentSettings.RememberPassword = rememberPassword;

            _manageSettings.CurrentUser = currentUser;

            _manageSettings.SaveCurrentSettings();

            return _manageSettings.CurrentUser;
        }

        #endregion
    }
}
